// 引入mysql的配置文件
const db = require('../../db.config');

// 引入sequelize对象
const Sequelize = db.sequelize;

// 引入数据表模型
const cosmeticsClassify = Sequelize.import('../../schema/base/cosmeticsClassify.js');
cosmeticsClassify.sync({force: false}); // 自动创建表

class CosmeticsClassifyModule {
  /**
   * 新增分类
   */
  static async addClassify(data) {
    cosmeticsClassify.create({
      cosmeticsClassify: data.classify,
      content: data.content
    })
  }

  /**
   * 查询分类
   */
  static async getClassify(where) {
    return cosmeticsClassify.findAll({
      where,
      raw: true
    })
  }

  /**
   * 删除分类
   */
  static async deleteClassify(where) {
    cosmeticsClassify.destroy({
      where
    })
  }

  /**
   * 更新content
   */
  static async updateClassify(classify, content) {
    let instance = await cosmeticsClassify.findOne({
      where: {
        cosmeticsClassify: classify
      }
    })
    instance.content = content
    await instance.save()
    return instance
  }
}

module.exports = CosmeticsClassifyModule
